home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / BoxTex / BoxTexShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-14  |  7.1 KB  |  291 lines  |  [TEXT/MPCC]

  1. //
  2. // This is box, the QuickDraw 3D starter program.  Written for the
  3. // Quickdraw 3D sample code
  4. //
  5. // This file contains utility routines for QuickDraw 3d sample code.
  6. // This app shows how to apply a texture shader to an object.  Bear in
  7. // mind that any object that you wish to texture map needs to have
  8. // UV parameters applied.
  9. //
  10. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  11. //
  12. // ©1994-5 Apple Computer Inc., All Rights Reserved
  13.  
  14.  
  15. // system headers
  16. #include <Desk.h>
  17. #include <Dialogs.h>
  18. #include <DiskInit.h>
  19. #include <Fonts.h>
  20. #include <Menus.h>
  21. #include <PictUtil.h>
  22. #include <QDOffScreen.h>
  23. #include <QuickDraw.h>
  24. #include <SegLoad.h>
  25. #include <StandardFile.h>
  26. #include <TextEdit.h>
  27.  
  28. // for QuickDraw 3D
  29. #include "QD3D.h"
  30. #include "QD3DMath.h"
  31. #include "QD3DDrawContext.h"
  32. #include "QD3DShader.h"
  33. #include "QD3DTransform.h"
  34. #include "QD3DGroup.h"
  35.  
  36.  
  37. #include "BoxTexShell.h"
  38. #include "BoxTex3DSupport.h"
  39. #include "Textures.h"
  40.  
  41. //-------------------------------------------------------------------------------------------
  42. // function prototypes
  43.  
  44. static void         InitToolbox( void ) ;
  45. static void         MainEventLoop( void ) ;
  46. static void         HandleKeyPress(EventRecord *event) ;
  47. static void         HandleOSEvent(EventRecord *event) ;
  48. void InitDocumentData( DocumentPtr theDocument ) ;
  49. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  50. void DisposeDocumentData( DocumentPtr theDocument) ;
  51.  
  52.  
  53. //-------------------------------------------------------------------------------------------
  54. //
  55.  
  56. Boolean         gQuitFlag         = false ;
  57. WindowPtr        gMainWindow        = nil ;
  58. DocumentRec        gDocument ;
  59.  
  60. //-------------------------------------------------------------------------------------------
  61. // main()
  62. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  63. // and enter the main event loop.  On exit from the main event loop, we want to call
  64. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  65.  
  66. void main(void)
  67. {
  68.     TQ3Status    myStatus;
  69.     Rect        rBounds = { 50, 50, 350, 350 } ;
  70.     Str255        title = "\pSpinning Box" ;
  71.  
  72.     InitToolbox() ;
  73.     
  74.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  75.     myStatus = Q3Initialize();
  76.     if ( myStatus == kQ3Failure )
  77.         DebugStr("\pErInitialize returned failure.");            
  78.  
  79.     // set up our globals
  80.     gQuitFlag = false ;
  81.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  82.  
  83.     InitDocumentData( &gDocument ) ;
  84.     
  85.     MainEventLoop();
  86.     
  87.     DisposeDocumentData( &gDocument ) ;
  88.     
  89.     //    Close our connection to the QuickDraw 3D library
  90.     myStatus = Q3Exit();
  91.     if ( myStatus == kQ3Failure )
  92.         DebugStr("\pErExit returned failure.");
  93.     
  94. }
  95.  
  96. //-------------------------------------------------------------------------------------------
  97. //
  98.  
  99. void InitDocumentData( DocumentPtr theDocument ) 
  100. {
  101.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  102.     
  103.     theDocument->fGroupScale = 1;                
  104.     theDocument->fGroupCenter = myOrigin ;            
  105.  
  106.     // sets up the 3d data for the scene
  107.     // Create view for QuickDraw 3D.
  108.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  109.  
  110.     // the main display group:
  111.     theDocument->fModel = MyNewModel() ;
  112.     
  113.     // the drawing styles:
  114.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  115.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  116.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  117.  
  118.     // set the rotation matrix the identity matrix
  119.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);
  120.  
  121.     AdjustCamera(    &gDocument, 
  122.                     (gMainWindow->portRect.right - gMainWindow->portRect.left),
  123.                     (gMainWindow->portRect.bottom - gMainWindow->portRect.top) ) ;
  124.                     
  125. }
  126.  
  127. void DisposeDocumentData( DocumentPtr theDocument)
  128. {
  129.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  130.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  131.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  132.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  133.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  134.  
  135. }
  136. //-----------------------------------------------------------------------------
  137. // 
  138.  
  139. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  140. {    
  141.     Q3View_StartRendering(theDocument->fView );
  142.     do {
  143.         SubmitScene( theDocument ) ;
  144.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  145.     return kQ3Success ;
  146. }
  147.  
  148. //-------------------------------------------------------------------------------------------
  149. //
  150.  
  151. short HiWrd(long aLong)
  152. {
  153.     return    (((aLong) >> 16) & 0xFFFF) ;
  154. }
  155.  
  156. //-------------------------------------------------------------------------------------------
  157. //
  158.  
  159. short LoWrd(long aLong)
  160. {
  161.     return    ((aLong) & 0xFFFF) ;
  162.  
  163. }
  164.  
  165. //-------------------------------------------------------------------------------------------
  166. //
  167.  
  168. void InitToolbox()
  169. {
  170.     Handle        menuBar = nil;
  171.  
  172.     MaxApplZone() ;
  173.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  174.     
  175.     InitGraf( &qd.thePort );
  176.     InitFonts();
  177.     InitWindows();
  178.     InitCursor();
  179.  
  180.     FlushEvents( everyEvent, 0 ) ;
  181.     // initialize application globals
  182.     
  183.     gQuitFlag = false;
  184.     
  185. }
  186.  
  187.  
  188. //-------------------------------------------------------------------------------------------
  189. //
  190. void MainEventLoop()
  191. {
  192.     EventRecord     event;
  193.     WindowPtr       window;
  194.     short           thePart;
  195.     Rect            screenRect, updateRect;
  196.     Point            aPoint = {100, 100};
  197.     
  198.  
  199.     while( !gQuitFlag )
  200.     {
  201.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  202.         {
  203.  
  204.             switch (event.what) {
  205.                 case mouseDown:
  206.                 
  207.                     thePart = FindWindow( event.where, &window );
  208.                     
  209.                     switch( thePart ) {
  210.                         case inMenuBar: 
  211.                             break;
  212.                         
  213.                         case inDrag:
  214.                     
  215.                             screenRect = (**GetGrayRgn()).rgnBBox;
  216.                             DragWindow( window, event.where, &screenRect );
  217.                             break ;
  218.                     
  219.                         case inContent:
  220.                     
  221.                             if (window != FrontWindow())
  222.                                 SelectWindow( window );
  223.                             break ;
  224.                     
  225.                         case inGoAway:
  226.                             if (TrackGoAway( window, event.where )) {
  227.                                 DisposeWindow ( window );
  228.                                 gQuitFlag = true;
  229.  
  230.                             }
  231.                             break ;
  232.                             
  233.                         default:
  234.                             break ;
  235.                     }
  236.                     break ;
  237.                             
  238.                         
  239.                 case updateEvt:
  240.                 
  241.                     window = (WindowPtr)event.message;
  242.                     updateRect = (**(window->visRgn)).rgnBBox;
  243.                     SetPort( window ) ;
  244.                     BeginUpdate( window );
  245.                     DocumentDraw3DData( &gDocument ) ;
  246.                     EndUpdate( window );
  247.                     break ;
  248.                     
  249.                 case keyDown:
  250.                 case autoKey:
  251.                     HandleKeyPress(&event);
  252.                     break;
  253.                     
  254.                 case diskEvt:
  255.                     if ( HiWrd(event.message) != noErr ) 
  256.                         (void) DIBadMount(aPoint, event.message);
  257.                     break;
  258.                     
  259.                 case osEvt:
  260.                 case activateEvt:
  261.                     break;
  262.  
  263.  
  264.             }
  265.         }
  266.         else {
  267.             // we received a null event, rotate the cube
  268.             TQ3Matrix4x4    tmp;
  269.             Rect        theRect = ((GrafPtr)gMainWindow)->portRect ;
  270.             
  271.             SetPort((GrafPtr)gMainWindow) ;
  272.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  273.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  274.  
  275.             InvalRect( &theRect ) ;
  276.         }
  277.     }
  278. }
  279.  
  280.  
  281. //-------------------------------------------------------------------------------------------
  282. //
  283. void HandleKeyPress(EventRecord *event)
  284. {}
  285.  
  286. //-------------------------------------------------------------------------------------------
  287. //
  288.  
  289.  
  290.  
  291.